package com.ruoyi.web.controller.danger;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.opio.util.ExampleBase;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.entity.SysFirePoint;
import com.ruoyi.system.service.SysFirePointService;
import com.ruoyi.system.service.impl.SysFirePointServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * (SysFirePoint)表控制层
 * @since 2022-12-01 16:50:06
 */
@RestController
@RequestMapping("sysFirePoint")
public class SysFirePointController extends BaseController {

    @Resource
    private SysFirePointService sysFirePointService;

    @Resource
    private SysFirePointServiceImpl sysFirePointServicei;

    /**
     * 分页查询
     * @param sysFirePoint 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(SysFirePoint sysFirePoint) {
        startPage();
        List<SysFirePoint> sysFirePoints = this.sysFirePointService.queryByPage(sysFirePoint);
        return getDataTable(sysFirePoints);
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.sysFirePointService.queryById(id));
    }

    /**
     * 新增数据
     * @param sysFirePoint 实体
     * @return 新增结果
     */
    @PostMapping("/create")
//    @PreAuthorize("@ss.hasPermi('sysFirePoint:create')")
    public AjaxResult add(@RequestBody SysFirePoint sysFirePoint) {
        return AjaxResult.success(this.sysFirePointService.insert(sysFirePoint));
    }

    /**
     * 编辑数据
     * @param sysFirePoint 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
//    @PreAuthorize("@ss.hasPermi('sysFirePoint:update')")
    public AjaxResult edit(@RequestBody SysFirePoint sysFirePoint) {
        return AjaxResult.success(this.sysFirePointService.update(sysFirePoint));
    }

    /**
     * 删除数据
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
//    @PreAuthorize("@ss.hasPermi('sysFirePoint:delete')")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.sysFirePointService.deleteById(id));
    }


    /**
     * 与实时数据库关联
     * @return
     */
    @GetMapping("/getTimeData")
    public AjaxResult getTimeData(SysFirePoint query) {
        List<SysFirePoint> sysFirePoints = this.sysFirePointService.queryByPage(query);
        Map<String, Object> map = ExampleBase.toResult();
        if (CollUtil.isNotEmpty(map)) {
            sysFirePoints.forEach(sysFirePoint -> {
                if (map.containsKey(sysFirePoint.getBitNumber())) {
                    Object o = map.get(sysFirePoint.getBitNumber());
                    sysFirePoint.setAv(o.toString());
                }
            });
        }
        return AjaxResult.success(sysFirePoints);
    }


    /**
     * 导入
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "火灾报警点位(", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<SysFirePoint> util = new ExcelUtil<>(SysFirePoint.class);
        List<SysFirePoint> sysFirePoints = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = sysFirePointService.importExcel(sysFirePoints, updateSupport, operName);
        return success(message);
    }

    /**
     * 导出
     * @param response
     * @param sysFirePoint
     */
    @Log(title = "火灾报警点位(", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysFirePoint sysFirePoint){
        List<SysFirePoint> sysFirePoints = sysFirePointService.queryByPage(sysFirePoint);
        ExcelUtil<SysFirePoint> util = new ExcelUtil<>(SysFirePoint.class);
        util.exportExcel(response, sysFirePoints, "火灾报警点位(");
    }

    /**
     *
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SysFirePoint> util = new ExcelUtil<>(SysFirePoint.class);
        util.importTemplateExcel(response,"火灾报警点位(");
    }
}

